//
//  MYHomeVC.m
//  MuYing
//
//  Created by tbb on 2019/7/18.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYHomeVC.h"
#import "MYLoginVC.h"
#import "MYHomeSearchVC.h"
#import "YQMYBaseNavigationController.h"
#import "YQNavTitleView.h"
#import "MYHomeSubclassVC.h"
#import <JXCategoryView/JXCategoryView.h>
#import "YQMYBaseNavigationController.h"
#import "MYHomeAttentionVC.h"
@interface MYHomeVC ()<JXCategoryViewDelegate,JXCategoryListContainerViewDelegate,UIScrollViewDelegate>
@property (nonatomic, strong) JXCategoryTitleView * categoryView;
@property (nonatomic, strong) JXCategoryListContainerView * listContainerView;
@property (nonatomic, copy) NSArray * titles;
@property (nonatomic, assign) NSInteger currentIndex;

@property (nonatomic, strong) UIButton * searchBtn;
@end

@implementation MYHomeVC

- (void)viewDidLoad {
    [super viewDidLoad];
}

-(void)setupView {
    [super setupView];
    self.view.backgroundColor = [UIColor whiteColor];
    self.titles = @[kYQLS(@"发现"),kYQLS(@"关注"),kYQLS(@"热话")];
    YQNavTitleView *navView = [YQNavTitleView new];
    navView.frame = CGRectMake(0, 0, kScreenWidth, 44);
    navView.intrinsicContentSize = CGSizeMake(kScreenWidth, 44);
    self.navigationItem.titleView = navView;
    
    UIView *bgView = [UIView new];
    [navView addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(kScreenWidth, 44));
    }];
    
    [bgView addSubview:self.searchBtn];
    [self.searchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-12);
        make.size.mas_equalTo(CGSizeMake(18, 18));
    }];
    
    self.categoryView = [[JXCategoryTitleView alloc] init];
    self.categoryView.delegate = self;
    self.categoryView.titles = self.titles;
    self.categoryView.defaultSelectedIndex = 0;
    //    self.categoryView.cellSpacing = 82;
    self.categoryView.titleSelectedColor = kAPPThemeColor;
    self.categoryView.contentEdgeInsetLeft = 0;
    self.categoryView.titleFont = kSystemMedium(17);
    self.categoryView.titleSelectedFont = kSystemMedium(17);
    self.categoryView.titleColor = HexColor(0x8F8F8F);
    self.categoryView.titleColorGradientEnabled = YES;

    self.categoryView.averageCellSpacingEnabled = YES;
    self.categoryView.backgroundColor = UIColor.whiteColor;
    JXCategoryIndicatorLineView *lineView = [[JXCategoryIndicatorLineView alloc] init];
    
    lineView.indicatorColor = kAPPThemeColor;
    lineView.indicatorWidth = 32;
    lineView.indicatorHeight = 2.0;
    lineView.verticalMargin = 2;
    self.categoryView.indicators = @[lineView];
    [bgView addSubview:self.categoryView];
    
    CGFloat width=200;
    
    if ([kYQLS(@"发现") isEqualToString:@"Discover"]) {
        width=kYQPScreenWidth-55;
    }
    
    [self.categoryView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(width, 44));
    }];

    self.listContainerView = [[JXCategoryListContainerView alloc] initWithDelegate:self];
    self.listContainerView.defaultSelectedIndex = 0;
    [self.view addSubview:self.listContainerView];
    self.listContainerView.scrollView.delegate = self;
    self.categoryView.contentScrollView = self.listContainerView.scrollView;
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
//    self.categoryView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 44);
    self.listContainerView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
}

#pragma mark - JXCategoryViewDelegate

- (void)categoryView:(JXCategoryBaseView *)categoryView didClickSelectedItemAtIndex:(NSInteger)index {
    [self.listContainerView didClickSelectedItemAtIndex:index];
}

- (void)categoryView:(JXCategoryBaseView *)categoryView scrollingFromLeftIndex:(NSInteger)leftIndex toRightIndex:(NSInteger)rightIndex ratio:(CGFloat)ratio {
    [self.listContainerView scrollingFromLeftIndex:leftIndex toRightIndex:rightIndex ratio:ratio selectedIndex:categoryView.selectedIndex];
}
#pragma mark - JXCategoryViewDelegate end
#pragma mark - JXCategoryListContainerViewDelegate

- (id<JXCategoryListContentViewDelegate>)listContainerView:(JXCategoryListContainerView *)listContainerView initListForIndex:(NSInteger)index {
    
    if (index == 0) {
        MYHomeSubclassVC *discoverVC = [[MYHomeSubclassVC alloc] init];
        discoverVC.type = 0;//发现
        [self addChildViewController:discoverVC];
        return discoverVC;
    }else if(index == 1){
        MYHomeAttentionVC *attentionVC = [[MYHomeAttentionVC alloc] init];
        attentionVC.channelId = @"0";
        [self addChildViewController:attentionVC];
//        attentionVC.view.backgroundColor = UIColor.lightGrayColor;
        return attentionVC;
    }else{
        MYHomeSubclassVC *hotVC = [[MYHomeSubclassVC alloc] init];
        hotVC.type = 1;//热话
        [self addChildViewController:hotVC];
        return hotVC;
    }

}

- (NSInteger)numberOfListsInlistContainerView:(JXCategoryListContainerView *)listContainerView {
    return self.titles.count;
}

#pragma mark - JXCategoryListContainerViewDelegate end

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if ([self isKindOfClass:[MYHomeVC class]]) {
        CGFloat index = scrollView.contentOffset.x/scrollView.bounds.size.width;
        CGFloat absIndex = fabs(index - self.currentIndex);
        if (absIndex >= 1) {
            //”快速滑动的时候，只响应最外层VC持有的scrollView“，说实话，完全可以不用处理这种情况。如果你们的产品经理坚持认为这是个问题，就把这块代码加上吧。
            //嵌套使用的时候，最外层的VC持有的scrollView在翻页之后，就断掉一次手势。解决快速滑动的时候，只响应最外层VC持有的scrollView。子VC持有的scrollView却没有响应
            self.listContainerView.scrollView.panGestureRecognizer.enabled = NO;
            self.listContainerView.scrollView.panGestureRecognizer.enabled = YES;
            _currentIndex = floor(index);
        }
    }
}
#pragma mark - UIScrollViewDelegate end

#pragma mark --lazy
-(UIButton *)searchBtn {
    if (!_searchBtn) {
        _searchBtn = [UIButton buttonWithType:0];
        [_searchBtn setImage:YQIMG(@"common_search_icon") forState:(UIControlStateNormal)];
        ws(self);
        [_searchBtn addTapAction:^(id  _Nonnull sender) {
            MYHomeSearchVC *searchVC = [[MYHomeSearchVC alloc] init];
            YQMYBaseNavigationController *nav = [[YQMYBaseNavigationController alloc] initWithRootViewController:searchVC];
            nav.modalPresentationStyle = UIModalPresentationOverFullScreen;
            [weakself.navigationController presentViewController:nav animated:true completion:nil];
        }];
    }
    return _searchBtn;
}
#pragma mark --lazy
@end
